// pages/result/result.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    marathon: "",
    results: {},
    promote: {},
    map: {},
    status: "-1",
    results_two: {},
    promote_two: {},
    select_tab_for_service: 0,
  },

  bindChange: function (e) {
    console.log(e)
    this.setData({
      select_tab_for_service: e.detail.current
    })
  },

  first_select_for_service: function () {
    this.setData({
      select_tab_for_service: 0
    })
  },

  second_select_for_service: function () {
    this.setData({
      select_tab_for_service: 1
    })
  },

  level_up: function () {
    var that = this
    var promote_groups = ""
    var promote = that.data.promote

    for (var dimen in promote) {
      for (var group in promote[dimen]) {
        if (promote[dimen][group]) {
          promote_groups += "," + group
        }
      }
    }

    var promote_groups = promote_groups.split(",")
    promote_groups.splice(0, 1)

    console.log(promote_groups.toString())

    //提交晋升队伍名单
    wx.request({
      url: getApp().globalData.my_server + "/hackson/promote",
      data: {
        marathon_id: that.data.marathon,
        promote: promote_groups.toString()
      },
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function (res) {
        console.log(res)
        if (res.data.error_no != 0) {
          wx.showModal({
            title: '提示',
            content: res.data.error_msg,
            success: function (res) {
              if (res.confirm) {
                console.log('用户点击确定')
              } else if (res.cancel) {
                console.log('用户点击取消')
              }
            }
          })
        } else if (res.data.error_no == 0) {
          wx.showModal({
            title: '提示',
            content: "提交晋升队伍名单成功！",
            success: function (res) {
              if (res.confirm) {
                console.log('用户点击确定')
                wx,wx.navigateBack({
                  delta: 1,
                })
              } else if (res.cancel) {
                console.log('用户点击取消')
                wx, wx.navigateBack({
                  delta: 1,
                })
              }
            }
          })
        }
      },
      fail: function (res) {
        wx.showModal({
          title: '哎呀～',
          content: '提交晋升队伍名单失败',
          success: function (res) {
            if (res.confirm) {
              console.log('用户点击确定')
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
        console.log("fail!", res)
      },
      complete: function (res) {

      }
    })

  },

  choose: function (e) {
    var that = this
    var dimen_index = e.currentTarget.dataset.dimen_index
    var index = e.currentTarget.dataset.index

    var promote_temp = that.data.promote

    promote_temp[dimen_index][index] = !promote_temp[dimen_index][index]

    that.setData({
      promote: promote_temp
    })

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    console.log(options)

    //查询此次马拉松结果
    wx.request({
      url: getApp().globalData.my_server + "/hackson/result",
      data: {
        marathon_id: options.marathon,
        status: options.status
      },
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function (res) {
        console.log(res)
        if (res.data.error_no != 0) {
          wx.showModal({
            title: '提示',
            content: res.data.error_msg,
            success: function (res) {
              if (res.confirm) {
                console.log('用户点击确定')
              } else if (res.cancel) {
                console.log('用户点击取消')
              }
            }
          })
        } else if (res.data.error_no == 0) {
          //排序
          var results_temp = res.data.results[0]

          for (var dimen in results_temp) {

            var dimen_temp = []

            for (var group in results_temp[dimen]) {
              dimen_temp.push({ [group]: results_temp[dimen][group] })
            }

            results_temp[dimen] = dimen_temp

          }

          for (var dimen in results_temp) {
            for (var i = 0; i < results_temp[dimen].length - 1; i++) {
              for (var j = 0; j < results_temp[dimen].length - 1 - i; j++) {
                if (results_temp[dimen][j][Object.keys(results_temp[dimen][j])[0]] < results_temp[dimen][j + 1][Object.keys(results_temp[dimen][j + 1])[0]]) {
                  var temp = results_temp[dimen][j]
                  results_temp[dimen][j] = results_temp[dimen][j + 1]
                  results_temp[dimen][j + 1] = temp
                }
              }
            }
          }

          for (var dimen in results_temp) {

            var dimen_temp2 = {}

            for (var i in results_temp[dimen]) {
              Object.assign(dimen_temp2, results_temp[dimen][i])
            }

            results_temp[dimen] = dimen_temp2

          }

          var promote_temp = {}
          for (var dimen in results_temp) {
            promote_temp[dimen] = {}

            for (var group in results_temp[dimen]) {
              promote_temp[dimen][group] = 0
            }
          }

          that.setData({
            marathon: options.marathon,
            status: options.status,
            results: results_temp,
            promote: promote_temp
          })

          if (that.data.status == "6") {
            //排序
            var results_temp = res.data.results[1]

            for (var dimen in results_temp) {

              var dimen_temp = []

              for (var group in results_temp[dimen]) {
                dimen_temp.push({ [group]: results_temp[dimen][group] })
              }

              results_temp[dimen] = dimen_temp

            }

            for (var dimen in results_temp) {
              for (var i = 0; i < results_temp[dimen].length - 1; i++) {
                for (var j = 0; j < results_temp[dimen].length - 1 - i; j++) {
                  if (results_temp[dimen][j][Object.keys(results_temp[dimen][j])[0]] < results_temp[dimen][j + 1][Object.keys(results_temp[dimen][j + 1])[0]]) {
                    var temp = results_temp[dimen][j]
                    results_temp[dimen][j] = results_temp[dimen][j + 1]
                    results_temp[dimen][j + 1] = temp
                  }
                }
              }
            }

            for (var dimen in results_temp) {

              var dimen_temp2 = {}

              for (var i in results_temp[dimen]) {
                Object.assign(dimen_temp2, results_temp[dimen][i])
              }

              results_temp[dimen] = dimen_temp2

            }

            var promote_temp = {}
            for (var dimen in results_temp) {
              promote_temp[dimen] = {}

              for (var group in results_temp[dimen]) {
                promote_temp[dimen][group] = 0
              }
            }

            that.setData({
              results_two: results_temp,
              promote_two: promote_temp
            })
          }

          //查询项目与学校对应关系
          wx.request({
            url: getApp().globalData.my_server + "/hackson/map_group_school",
            data: {
              marathon_id: options.marathon
            },
            method: "POST",
            header: {
              "Content-Type": "application/x-www-form-urlencoded"
            },
            success: function (res) {
              console.log(res)
              if (res.data.error_no != 0) {
                wx.showModal({
                  title: '提示',
                  content: res.data.error_msg,
                  success: function (res) {
                    if (res.confirm) {
                      console.log('用户点击确定')
                    } else if (res.cancel) {
                      console.log('用户点击取消')
                    }
                  }
                })
              } else if (res.data.error_no == 0) {
                that.setData({
                  map: res.data.map
                })
              }
            },
            fail: function (res) {
              wx.showModal({
                title: '哎呀～',
                content: '查询项目与学校对应关系失败',
                success: function (res) {
                  if (res.confirm) {
                    console.log('用户点击确定')
                  } else if (res.cancel) {
                    console.log('用户点击取消')
                  }
                }
              })
              console.log("fail!", res)
            },
            complete: function (res) {

            }
          })

        }
      },
      fail: function (res) {
        wx.showModal({
          title: '哎呀～',
          content: '查询此次马拉松结果失败',
          success: function (res) {
            if (res.confirm) {
              console.log('用户点击确定')
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
        console.log("fail!", res)
      },
      complete: function (res) {

      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})